/**
 *     This file is part of cesm-ws.
 *
 *     cesm-ws, Web Service for Community Earth System Model
 *
 *     Copyright (c) 2010, Purdue University and/or affilicates or thrid-party contributors as
 *     indicated by the @author tags or express copyright attribution statements
 *     applied by the authors. All third-party contributions are distributed under
 *     license by Purdue University.
 *
 *     cesm-ws is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this distribution.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.purdue.rcac.grid.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataSource;

public class ByteArrayDataSource implements DataSource {
	private byte[] bytes;
	private String contentType;
	private int length;
	
	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}

	public byte[] getBytes() {
		return bytes;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentType() {
		return contentType;
	}

	public InputStream getInputStream() {
		return new ByteArrayInputStream(bytes, 0, length);
	}
	
	public ByteArrayDataSource(byte[] bytes, int length, String contentType) {
		super();
		this.bytes = bytes;
		this.contentType = contentType;
		this.length = length;
	}

	/**
	 * for completeness, here's how to implement the outputstream. this is
	 * unnecessary for what you're doing, you can just throw an
	 * UnsupportedOperationException.
	 */
	public OutputStream getOutputStream() {
		// final ByteArrayDataSource bads = this;
		// final ByteArrayOutputStream baos =
		// new ByteArrayOutputStream();
		// // return an outputstream that sets my byte array
		// // when it is closed.
		// return new FilterOutputStream(baos) {
		// public void close() throws IOException {
		// baos.close();
		// bads.setBytes(baos.toByteArray());
		// }
		// }
		throw new UnsupportedOperationException("not supported getOutputStream");
	}

	public String getName() {
		return "byte-array-DS";
	}
}
